package com.apex.crm.service;


import com.apex.crm.base.BaseService;
import com.apex.crm.dao.CustomerLinkmanMapper;
import com.apex.crm.utils.AssertUtil;
import com.apex.crm.utils.PhoneUtil;
import com.apex.crm.vo.CustomerLinkman;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname CustomerLinkmanService
 * @description TODO
 * @date 2021/6/9 14:36
 */
@Service
public class CustomerLinkmanService extends BaseService<CustomerLinkman,Integer> {

    @Resource
    private CustomerLinkmanMapper customerLinkmanMapper;

    public CustomerLinkman selectByCusId(Integer cid) {
        return customerLinkmanMapper.selectByCusId(cid);
    }


    public void updateCustomerLinkman(CustomerLinkman customerLinkman) {
        //参数校验
        checkParams(customerLinkman.getLinkName(),customerLinkman.getPhone(), customerLinkman.getPosition());
        //查找是否存在
        //CustomerLinkman temp = customerLinkmanMapper.selectOne(new QueryWrapper<CustomerLinkman>().eq("cus_id",customerLinkman.getCusId()));
        CustomerLinkman temp = customerLinkmanMapper.selectByCusId(customerLinkman.getCusId());
        AssertUtil.isTrue(null == temp,"待更新的客户记录不存在!");
        //更新时间
        customerLinkman.setUpdateDate(new Date());
        AssertUtil.isTrue(customerLinkmanMapper.updateByPrimaryKey(customerLinkman) < 1,"客户记录更新失败!");


    }

    //参数校验
    private void checkParams(String name, String phone, String legalPerson) {
        AssertUtil.isTrue(StringUtils.isBlank(name),"请指定联系人名称!");
        AssertUtil.isTrue(!(PhoneUtil.isMobile(phone)),"手机号格式非法!");
        AssertUtil.isTrue(StringUtils.isBlank(legalPerson),"请指定联系人职位!");
    }

}
